package com.eryos.android.cigarettecounter.io;

import android.content.SharedPreferences;
import android.util.Log;

/**
 * Project : CigaretteCounter Author : Olivier Combe URL : http://eryos.fr/CigaretteCounter/
 * 
 * Copyright(C)2011 - Olivier Combe
 * 
 * Licensed under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * 
 * @version \$Revision.*\$
 * @author Olivier Combe
 */
public class Preferences {

    /** Logger Tag. */
    private static final String LOG_TAG = "Preferences";

    private static final String PREF_SCREEN_WIDTH = "screenWidth";
    private static final String PREF_SCREEN_HEIGHT = "screenHeight";

    private static final String PREF_BOX_SIZE = "boxSize";
    private static final String PREF_BOX_PRICE = "boxPrice";
    private static final String PREF_CIGARETTE_PRICE = "cigarettePrice";

    /** Constant : Low usage. */
    private static final String MEDIUM_USAGE_LOW = "MEDIUM_USAGE_LOW";
    /** Constant : High usage. */
    private static final String MEDIUM_USAGE_HIGH = "MEDIUM_USAGE_HIGH";

    private int boxSize = -1; // Quantity of cigarettes in a box
    private float boxPrice = -1f; // Price of the box
    private float cigarettePrice = 0f; // boxPrice / boxSize;

    private int screenWidth = 0;
    private int screenHeight = 0;

    private int mediumUsageLow = 5;
    private int mediumUsageHigh = 10;

    private boolean lock = true;

    private SharedPreferences settings;

    /**
     * Default Constructor.
     * 
     * @param settingsArg
     *            to store in preferences
     */
    public Preferences(final SharedPreferences settingsArg) {
        settings = settingsArg;
    }

    /**
     * Store static param into preferences file.
     */
    public void storeParamToFile() {

        SharedPreferences.Editor editor = settings.edit();

        if (boxSize != -1) {
            editor.putInt(PREF_BOX_SIZE, boxSize);
        }
        if (boxPrice != -1f) {
            editor.putFloat(PREF_BOX_PRICE, boxPrice);
        }
        if (cigarettePrice != -1f) {
            editor.putFloat(PREF_CIGARETTE_PRICE, cigarettePrice);
        }
        if (screenWidth != -1) {
            editor.putInt(PREF_SCREEN_WIDTH, screenWidth);
        }
        if (screenHeight != -1) {
            editor.putInt(PREF_SCREEN_HEIGHT, screenHeight);
        }
        if (mediumUsageLow != -1) {
            editor.putInt(MEDIUM_USAGE_LOW, mediumUsageLow);
        }
        if (mediumUsageHigh != -1) {
            editor.putInt(MEDIUM_USAGE_HIGH, mediumUsageHigh);
        }

        // editor.putBoolean(PREF_TEST_UNLOCK, lock);

        editor.commit();
    }

    /**
     * Read static param from preferences file.
     */
    public void readParamFromFile() {
        Log.d(LOG_TAG, "readParamFromFile()");
        try {

            boxSize = settings.getInt(PREF_BOX_SIZE, -1);
            boxPrice = settings.getFloat(PREF_BOX_PRICE, -1f);
            cigarettePrice = settings.getFloat(PREF_CIGARETTE_PRICE, 0);
            // lock = settings.getBoolean(PREF_TEST_UNLOCK, true);

            mediumUsageLow = settings.getInt(MEDIUM_USAGE_LOW, 5);
            mediumUsageHigh = settings.getInt(MEDIUM_USAGE_HIGH, 10);

        } catch (Exception e) {
            Log.e(LOG_TAG, "readParamFromFile()", e);
            boxSize = -1;
            boxPrice = -1f;
            cigarettePrice = 0;
            lock = true;
        }
    }

    /**
     * Read static param from preferences file.
     */
    public void readExtraParamFromFile() {
        Log.d(LOG_TAG, "readExtraParamFromFile()");

        readParamFromFile();
        try {
            screenWidth = settings.getInt(PREF_SCREEN_WIDTH, -1);
            screenHeight = settings.getInt(PREF_SCREEN_HEIGHT, -1);

        } catch (Exception e) {
            Log.e(LOG_TAG, "readExtraParamFromFile()", e);
            screenWidth = -1;
            screenHeight = -1;
        }
    }

    /**
     * @return the boxSize
     */
    public int getBoxSize() {
        return boxSize;
    }

    /**
     * @param boxSizeArg
     *            the boxSize to set
     */
    public void setBoxSize(final int boxSizeArg) {
        this.boxSize = boxSizeArg;
    }

    /**
     * @return the boxPrice
     */
    public float getBoxPrice() {
        return boxPrice;
    }

    /**
     * @param boxPriceArg
     *            the boxPrice to set
     */
    public void setBoxPrice(final float boxPriceArg) {
        this.boxPrice = boxPriceArg;
    }

    /**
     * @return the cigarettePrice
     */
    public float getCigarettePrice() {
        return cigarettePrice;
    }

    /**
     * @param cigarettePriceArg
     *            the cigarettePrice to set
     */
    public void setCigarettePrice(final float cigarettePriceArg) {
        this.cigarettePrice = cigarettePriceArg;
    }

    /**
     * @return the screenWidth
     */
    public int getScreenWidth() {
        return screenWidth;
    }

    /**
     * @param screenWidthArg
     *            the screenWidth to set
     */
    public void setScreenWidth(final int screenWidthArg) {
        this.screenWidth = screenWidthArg;
    }

    /**
     * @return the screenHeight
     */
    public int getScreenHeight() {
        return screenHeight;
    }

    /**
     * @param screenHeightArg
     *            the screenHeight to set
     */
    public void setScreenHeight(final int screenHeightArg) {
        this.screenHeight = screenHeightArg;
    }

    /**
     * @return the mediumUsageLow
     */
    public int getMediumUsageLow() {
        return mediumUsageLow;
    }

    /**
     * @param mediumUsageLowArg
     *            the mediumUsageLow to set
     */
    public void setMediumUsageLow(final int mediumUsageLowArg) {
        this.mediumUsageLow = mediumUsageLowArg;
    }

    /**
     * @return the mediumUsageHigh
     */
    public int getMediumUsageHigh() {
        return mediumUsageHigh;
    }

    /**
     * @param mediumUsageHighArg
     *            the mediumUsageHigh to set
     */
    public void setMediumUsageHigh(final int mediumUsageHighArg) {
        this.mediumUsageHigh = mediumUsageHighArg;
    }

    /**
     * @return the lock
     */
    public boolean isLock() {
        return lock;
    }

    /**
     * @param lockArg
     *            the lock to set
     */
    public void setLock(final boolean lockArg) {
        this.lock = lockArg;
    }

    /**
     * @return the settings
     */
    public SharedPreferences getSettings() {
        return settings;
    }

    /**
     * @param settingsArg
     *            the settings to set
     */
    public void setSettings(final SharedPreferences settingsArg) {
        this.settings = settingsArg;
    }

}
